“取出数据表中第10条到第20条记录”的sql语句+select top 使用方法 您所在的位置:网站首页 sql 前10条 “取出数据表中第10条到第20条记录”的sql语句+select top 使用方法

“取出数据表中第10条到第20条记录”的sql语句+select top 使用方法

2023-08-25 07:28| 来源: 网络整理| 查看: 265

1.首先。select top使用方法:

參考问题  select top n * from和select * from的差别

select * from table --  取全部数据。返回无序集合

select top n * from table  -- 依据表内数据存储顺序取前n条,返回无序集合

select * from table order by id desc -- 取全部数据。按id逆序返回有序列表

select top n * from table order by id desc-- 先按id逆序。再取前n条,返回按id排序的有序集合【注意,按某个属性排序。该排序属性的数据列值最好是不反复的。假设有反复的。那排序属性值相等的这些行在结果集中的顺序事先是不能确定的】

  栗子例如以下~

 

我们以pid作为排序属性值,第16行,第19行和第20行的pid值相等。

如今取以pid排序的倒数5条记录:

Connection con=new SQLConnection().getConnection(); String sql="select top 5 * from test order by pid desc";

System.out.println("select begins...");

Statement statement=con.createStatement();   ResultSet result =  statement.executeQuery(sql);   while (result.next()) {           System.out.println(result.getInt(1)+","+result.getString(2)+","+result.getString(3));    }  System.out.println("select ends...");  con.close();  statement.close();  result.close();  con=null;  statement=null;  result=null;

结果:

select begins... 3,as,9 16,tt,8  【三者顺序事先不能确定】 19,gh,8 20,jk,8 6,bb,7 select ends...

2. 类似于“查询第10条到第20条记录”的sql语句写法 ===  常应用于分页显示上 1) String sql="select  top 10 * from (select * from test where id


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有